//
//  HistoryViewController.swift
//  MyProject
//
//  Created by volantPig on 2020/11/12.
//

import UIKit

class HistoryViewController: UIViewController {

    var historyList: [HistoryData]?
    
    var myTableView: UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initData()
        self.createUIElements()
        // Do any additional setup after loading the view.
    }
    
    // 初始化数据
    func initData() {
        self.historyList =  DBManager.default.getAllLocalData()
        
        print("\(self.historyList)")
    }
    
    /// 布局UI元素
    func createUIElements() {
        myTableView = UITableView.init(frame: .zero, style: .plain)
        self.view.addSubview(myTableView!)
        myTableView?.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            myTableView!.leftAnchor.constraint(equalTo: self.view.leftAnchor),
            myTableView!.topAnchor.constraint(equalTo: self.view.topAnchor),
            myTableView!.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
            myTableView!.rightAnchor.constraint(equalTo: self.view.rightAnchor)
        ])
        myTableView?.delegate = self
        myTableView?.dataSource = self
        myTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension HistoryViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.historyList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell")
        
        for subV in (cell?.subviews ?? [UIView]()) {
            subV.removeFromSuperview()
        }
        
        let model = self.historyList?[indexPath.row]
        
        let keyLabel = UILabel()
        keyLabel.translatesAutoresizingMaskIntoConstraints = false
        cell?.addSubview(keyLabel)

        NSLayoutConstraint.activate([
            keyLabel.leftAnchor.constraint(equalTo: cell!.leftAnchor, constant: 10),
            keyLabel.rightAnchor.constraint(equalTo: cell!.rightAnchor, constant: -10),
            keyLabel.topAnchor.constraint(equalTo: cell!.topAnchor, constant: 10)
        ])
        keyLabel.textColor = .black
        keyLabel.font = UIFont.systemFont(ofSize: 16)
        keyLabel.text = String.init(format: "获取数据时间：%@", model?.dateKey ?? "数据异常")
        keyLabel.numberOfLines = 0
        
        let valueLabel = UILabel()
        valueLabel.translatesAutoresizingMaskIntoConstraints = false
        cell?.addSubview(valueLabel)
        NSLayoutConstraint.activate([
            valueLabel.leftAnchor.constraint(equalTo: cell!.leftAnchor, constant: 10),
            valueLabel.rightAnchor.constraint(equalTo: cell!.rightAnchor, constant: -10),
            valueLabel.topAnchor.constraint(equalTo: keyLabel.bottomAnchor, constant: 10),
            valueLabel.bottomAnchor.constraint(equalTo: cell!.bottomAnchor, constant: -10)
        ])
        valueLabel.textColor = .black
        valueLabel.font = UIFont.systemFont(ofSize: 16)
        valueLabel.text = String.init(format: "获取数据内容：%@", model?.dateValue ?? "数据异常")
        valueLabel.numberOfLines = 0
        
        
        return cell!
    }
    
}
